/**
 * 
 */
package by.imotaeva.printing_services.services.impl;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import by.imotaeva.printing_services.dataaccess.BookOrderDao;
import by.imotaeva.printing_services.dataaccess.PrintingProductOrderDao;
import by.imotaeva.printing_services.datamodel.BookOrder;
import by.imotaeva.printing_services.datamodel.PrintingProductOrder;
import by.imotaeva.printing_services.services.BookOrderService;

/**
 * @author Inna
 *
 */
@Service
public class BookOrderServiceImpl implements BookOrderService {
	private static final Logger LOGGER = LoggerFactory
			.getLogger(BookOrderServiceImpl.class);

	@Inject
	private BookOrderDao dao;

	@Inject
	private PrintingProductOrderDao printingDao;

	@PostConstruct
	private void init() {
		// this method will be called by Spring after bean instantiation. Can be
		// used for any initialization process.
		LOGGER.info("Instance of BookOrderService is created. Class is: {}",
				getClass().getName());
	}

	@Override
	public void createNewBookOrder(BookOrder bookOrder,
			PrintingProductOrder printingProductOrder) {
		Validate.isTrue(
				bookOrder.getId() == null,
				"This method should be called for 'not saved yet' bookOrder only. Use UPDATE instead");
		printingDao.insert(printingProductOrder);
		bookOrder.setPrintingProductOrder(printingProductOrder);
		dao.insert(bookOrder);
	}

	@Override
	public void update(BookOrder entity) {
		dao.update(entity);

	}

	@Override
	public BookOrder get(Long id) {
		return dao.getById(id);

	}

	@Override
	public void removeBookOrder(Long id) {
		dao.delete(id);
	}

	@Override
	public List<BookOrder> getAllBookOrders() {
		return dao.getAll();
	}

	@Override
	public void deleteAllBookOrders() {
		LOGGER.debug("Remove all Book orders");
		dao.deleteAll();

	}

	@Override
	public BookOrder getFullBookOrder(Long id) {
		return dao.getFullBookOrder(id);
	}

	@Override
	public List<Long> getBookOrderIds() {
		return dao.getBookOrderIds();
	}

	@Override
	public Long getCountBookOrders() {
		return dao.getCount();
	}

}
